﻿//----------------------------------------------------------------------------------------------------------------------
// <copyright file="BaseViewModel.cs" company="Keith B. Rimington">
//
//   Copyright (c) 2011 Keith B. Rimington
//
//   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//     documentation files (the "Software"), to deal in the Software without restriction, including without limitation
//     the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
//     to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be included in all copies or substantial portions of
//     the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//     THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
//     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//     DEALINGS IN THE SOFTWARE.
//
// </copyright>
//----------------------------------------------------------------------------------------------------------------------

namespace Noc.Pomodoro.ViewModels
{
    using System.Collections;
    using System.ComponentModel;

    /// <summary>
    /// Base class for view models, providing a convenient backing store and interface
    /// for data binding and property change notification.
    /// </summary>
    public abstract class BaseViewModel : INotifyPropertyChanged, INotifyPropertyChanging
    {
        /// <summary>
        /// Default property backing store.
        /// </summary>
        private readonly Hashtable propertyStore = new Hashtable();

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Occurs when a property value is changing.
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;

        /// <summary>
        /// Gets the value of the property with the specified name.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="defaultValue">Default value if the property is <c>null</c>.</param>
        /// <returns>The value of the specified property.</returns>
        protected virtual TValue GetPropertyValue<TValue>(string propertyName, TValue defaultValue)
        {
            var value = (TValue)(this.propertyStore[propertyName] ?? defaultValue);
            return value;
        }

        /// <summary>
        /// Sets the value of the property with the specified name.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="value">The value to assign to the property.</param>
        protected virtual void SetPropertyValue<TValue>(string propertyName, TValue value)
        {
            var originalValue = this.GetPropertyValue(propertyName, default(TValue));
            if ((originalValue == null ^ value == null) ||
                (originalValue != null && value != null && !originalValue.Equals(value)))
            {
                if (this.PropertyChanging != null)
                {
                    this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
                }

                this.propertyStore[propertyName] = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
}